package com.example.administrator.android2019.ch3Activity;

import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.administrator.android2019.R;

import java.util.HashMap;

public class QQActivity extends AppCompatActivity {
     private EditText ed1,ed2;
     private Button btn1;

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_qq);
        btn1=findViewById(R.id.btn_qq);
        ed1=findViewById(R.id.ed_per);
        ed2=findViewById(R.id.ed_pas);
        /*用文件外部存储，先动态申请权限*/

        String []p={"android.permission.WRITE_EXTERNAL_STORAGE","android.permission.READ_EXTERNAL_STORAGE"};
        ActivityCompat.requestPermissions(QQActivity.this,p,1);
        /*从sp/文件中获取账号密码并显示在输入框*/
        HashMap<String ,String> hashMap=FileSaveQQ.get(QQActivity.this);
        ed1.setText(hashMap.get("name"));
        ed2.setText(hashMap.get("pwd"));
        View.OnClickListener click=new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                /*1、若输入框为空，则Toast提示不能空*/
                /*2、若不为空，将账号密码保存：sp  file*/
                String name=ed1.getText().toString().trim();//trim 去除空格
                String pwd=ed2.getText().toString().trim();
                if (TextUtils.isEmpty(name)||TextUtils.isEmpty(pwd)){
                    Toast.makeText(QQActivity.this,"账号或密码不能为空",Toast.LENGTH_SHORT).show();
                }else {
                     /*调用SPSaveQQ中的方法完成：将账号密码保存sp*/
                    boolean flag=FileSaveQQ.save(QQActivity.this,name,pwd);
                    if (flag){
                        Toast.makeText(QQActivity.this,"保存成功",Toast.LENGTH_SHORT).show();
                    }else {
                        Toast.makeText(QQActivity.this,"保存失败",Toast.LENGTH_SHORT).show();
                    }
                }
            }
        };
        btn1.setOnClickListener(click);

    }
}
